package com.star.book.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.google.common.collect.Maps;
import com.star.book.entity.User;
import com.star.book.mapper.UserMapper;
import com.star.book.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr Kong
 * @since 2020-10-03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IUserService userService;

    @Override
    public int selectUserStatusCount() {
        return baseMapper.selectUserStatusCount();
    }

    @Override
    public int selectUserCount() {
        return baseMapper.selectUserCount();
    }

    @Override
    public List<User> selectUserJiFen() {
        return baseMapper.selectUserJiFen();
    }

    @Override
    public List<User> findByPage(Integer id) {
        Integer current;
        if(id == 0){
            current = 0;
        }else if(id < 0){
            current =  0;
        }else{
            current =  id * 10;
        }
        List<User> byPage = baseMapper.findByPage(current);
        if(byPage.size() > 0){
            for (int i = 0; i < byPage.size(); i++) {
                String userSex = byPage.get(i).getUserSex();
                String userType = byPage.get(i).getUserType();
                if("1".equals(userType)){
                    byPage.get(i).setUserStatus("管理员");
                }else{
                    byPage.get(i).setUserStatus("用户");
                }
                if("1".equals(userSex)){
                    byPage.get(i).setUserSex("男");
                }else{
                    byPage.get(i).setUserSex("女");
                }
            }
        }
        return byPage;
    }

    @Override
    public Map<String, Object> userManager(Integer id) {
        Map<String, Object> user = Maps.newHashMap();
        //获取总共的用户数量
        List<User> list = userService.list();
        int userTotalCount = list.size();
        Integer page ;
        if(userTotalCount == 0 || userTotalCount < 0){
            page = 0;
        }else{
            page = userTotalCount / 10;
            if(userTotalCount % 10 > 0){
                page=page+1;
            }
        }
        user.put("page",page);
        List<User> byPage = baseMapper.findByPage(id);
        user.put("user",byPage);
        return user;
    }


}
